import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";
/**
 * 部门管理
 * guopeng
 * 2017.11.27
 */
export class Department{
  constructor(
    private departmentId : string,//部门id
    private departmentName : string,//部门名
    private departmentUrl : string,//部门地址
    private departmentDesc : string,//部门描述
    private departmentIcon : string,//部门图标
    private departmentStatus :string,//部门状态，0：启用，1：停用
    private departmentOrder :string,//部门序号
    private createTime : string,//创建时间
    private updateTime : string,//更新时间
    private extensionInfo : string//拓展信息
  ){}
}

@Component({
  selector: 'app-collectData',
  templateUrl: './department.component.html',
  styleUrls: ['./department.component.css']
})
export class DepartmentComponent implements OnInit{
  private departmentName : string;//部门名
  private departmentStatus :string;//部门状态，0：启用，1：停用
  errorMessage:string;//錯誤消息
  private list:Array<any>;
  public queryMessage:Department = new Department("","","","","","","","","","");
  public updateMessages:Department = new Department("","","","","","","","","","");
  public addMessages:Department =  new Department("","","","","","","","","","");
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService) {
  }
  add(){
    this.addMessages =  new Department("","","","","","","","","","");
  }

  userMessage(event:Department){
    let query = new Department("","","","","","","","","","");
    let update = new Department("","","","","","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let departmentName = info.departmentName== undefined ? '' : info.departmentName ;
    let departmentStatus = info.departmentStatus== undefined ? '' : info.departmentStatus;
    this.http.get(this.url+'/department/queryDepartmentListByPage?departmentName='+departmentName+'&departmentStatus='+departmentStatus).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/department/adddepartment',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101120001'){
          this.errorMessage = "部门名为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101120002'){
          this.errorMessage = "部门状态为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101120003'){
          this.errorMessage = "部门序号为空";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/department/deleteDepartment/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101120004'){
        this.errorMessage = "该部门不存在";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/department/updateDepartment',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101120001'){
          this.errorMessage = "部门名为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101120002'){
          this.errorMessage = "部门状态为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101120003'){
          this.errorMessage = "部门序号为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101120004'){
          this.errorMessage = "该部门不存在";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let departmentName = this.departmentName== undefined ? '' : this.departmentName ;
    let departmentStatus = this.departmentStatus== undefined ? '' : this.departmentStatus;
    let current = pageNo;
    this.http.get(this.url+'/department/queryDepartmentListByPage?departmentName='+departmentName+'&departmentStatus='+departmentStatus+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/department/queryDepartmentListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}


